#include "BasePassRendering.h"

namespace Alice
{
    static VertexShader* sVertexShader=nullptr;
    static PixelShader* sPixelShader=nullptr;
    FBasePassMeshProcessor::FBasePassMeshProcessor(EMeshPass::Type InMeshPassType, const Scene* InScene, const SceneView* InViewIfDynamicMeshCommand, const FMeshPassProcessorRenderState& InDrawRenderState, FMeshPassDrawListContext* InDrawListContext, EFlags Flags, ETranslucencyPass::Type InTranslucencyPassType)
    : FMeshPassProcessor(InMeshPassType, InScene, InViewIfDynamicMeshCommand, InDrawListContext)
    , PassDrawRenderState(InDrawRenderState)
    , TranslucencyPassType(InTranslucencyPassType)
    , bTranslucentBasePass(InTranslucencyPassType != ETranslucencyPass::TPT_MAX)
    , bOITBasePass(InTranslucencyPassType != ETranslucencyPass::TPT_MAX)// && OIT::IsSortedPixelsEnabled(GetFeatureLevelShaderPlatform(InFeatureLevel)))
    , bEnableReceiveDecalOutput((Flags & EFlags::CanUseDepthStencil) == EFlags::CanUseDepthStencil)
    , EarlyZPassMode(DDM_None)//(InScene ? InScene->EarlyZPassMode : DDM_None)
    , bRequiresExplicit128bitRT((Flags & EFlags::bRequires128bitRT) == EFlags::bRequires128bitRT)
    {
        
    }
    void FBasePassMeshProcessor::AddMeshBatch(const FMeshBatch& inMeshBatch, uint64 BatchElementMask, const PrimitiveSceneProxy* PrimitiveSceneProxy, int32 StaticMeshId)
    {
        const MaterialRenderProxy* materialRenderProxy = inMeshBatch.mMaterialRenderProxy;
        const MaterialShared* materialShared=materialRenderProxy->GetMaterialNoFallback();
        TryAddMeshBatch(inMeshBatch,BatchElementMask,PrimitiveSceneProxy,0,*materialRenderProxy,*materialShared);
    }
    bool FBasePassMeshProcessor::TryAddMeshBatch(const FMeshBatch& inMeshBatch, uint64 BatchElementMask, const PrimitiveSceneProxy* PrimitiveSceneProxy, int32 StaticMeshId, const MaterialRenderProxy& MaterialRenderProxy, const MaterialShared& inMaterial)
    {
        FMeshProcessorShaders meshProcessorShaders;
        FMeshPassProcessorRenderState meshPassProcessorRenderState;
        FMeshDrawCommandSortKey meshDrawCommandSortKey;
        int a=0;
        BuildMeshDrawCommands(inMeshBatch,BatchElementMask,PrimitiveSceneProxy,MaterialRenderProxy,inMaterial,meshPassProcessorRenderState,meshProcessorShaders,ERasterizerFillMode::FM_Solid,ERasterizerCullMode::CM_CCW,meshDrawCommandSortKey,EMeshPassFeatures::Default,a);
        return true;
    }
    FMeshPassProcessor* CreateBasePassProcessor(const Scene* Scene, const SceneView* InViewIfDynamicMeshCommand, FMeshPassDrawListContext* InDrawListContext)
    {
        //const FExclusiveDepthStencil::Type DefaultBasePassDepthStencilAccess = Scene::GetDefaultBasePassDepthStencilAccess(FeatureLevel);
	
        FMeshPassProcessorRenderState PassDrawRenderState;
        //SetupBasePassState(DefaultBasePassDepthStencilAccess, false, PassDrawRenderState);

        const FBasePassMeshProcessor::EFlags Flags = FBasePassMeshProcessor::EFlags::CanUseDepthStencil;

        return new FBasePassMeshProcessor(EMeshPass::BasePass, Scene, InViewIfDynamicMeshCommand, PassDrawRenderState, InDrawListContext, Flags);
    }
    REGISTER_MESHPASSPROCESSOR(BasePass, CreateBasePassProcessor, EShadingPath::Deferred, EMeshPass::BasePass, EMeshPassFlags::CachedMeshCommands | EMeshPassFlags::MainView);
}